package com.abe.config;

import java.util.ArrayList;
import java.util.List;

import com.abe.app.ManagerAPP;
import com.abe.mvp.model.bean.CurrentUserBean;
import com.abe.mvp.model.bean.MenuBean;
import com.abe.mvp.model.bean.SettingBean;
import com.abe.mvp.model.bean.http.UserBean;
import com.abe.threadmill.R;
import com.abe.utils.JsonHelper;
import com.abe.utils.NormalUtils;
import com.abe.utils.SPUtils;

/** 应用设置参数相关 */
public class SettingParam {
	/** 支持语言 */
	public static final String LANGUAGE_KEY_CN = "LANGUAGE_KEY_CN";
	public static final String LANGUAGE_KEY_EN = "LANGUAGE_KEY_EN";

	/** SP缓存字段名称 */
	/** 当前配置参数 */
	public static final String SP_CurrentSetting = "CurrentSetting";
	/** 当前登录用户 */
	public static final String SP_CurrentUser = "CurrentUser";
	/** 获取系统的语言 */
	public static final String SP_LANGUAGE_KEY = "LANGUAGE_KEY";

	/** 锻炼用户类型 0：游客 1：扫码登录用户 2：刷卡登录用户 */
	public static final int flag_user_tourist = 0;
	public static final int flag_user_scanlogin = 1;
	public static final int flag_user_swingcard = 2;

	/** @see 支持 @see 0：无 @see 1：扫码登录 @see 2：刷卡登录 @see 3：刷卡与扫码均支持 */
	public static final int loginType_no = 0;
	public static final int loginType_scan = 1;
	public static final int loginType_swingcard = 2;
	public static final int loginType_all = 3;

	/** 初始密码 */
	public static final String initial_password = "123456";

	/** 设备型号 */
	public static final String[] device_type = { "设备1" };

	/** 支持语言 */
	public static final String[] languages = { LANGUAGE_KEY_CN, LANGUAGE_KEY_EN };

	/** WIFI设置 */
	public static final String socket = "socket";
	public static final String wifi = "wifi";
	public static final String[] wifis = { socket, wifi };

	public static String getStrigById(int stringid) {
		return ManagerAPP.getContext().getResources().getString(stringid);
	}

	/** 游客 */
	public static CurrentUserBean getTouristUser() {
		CurrentUserBean bean = new CurrentUserBean();
		bean.setFlag(flag_user_tourist);
		UserBean user = new UserBean();
		user.setHeadimgurl("");
		user.setOpenid("");
		user.setUserName(NormalUtils.getString(R.string.user_tourist));
		bean.setUser(user);
		return bean;
	}

	/** 默认设置 */
	public static SettingBean getDefaultSettingParam() {
		SettingBean bean = new SettingBean();
		if (device_type.length == 0) {
			bean.setDeviceType("");
		} else {
			bean.setDeviceType(device_type[0]);
		}
		bean.setLanguage(LANGUAGE_KEY_CN);
		bean.setLoginType(loginType_no);
		bean.setSetPassword(initial_password);
		bean.setSupportTourist(false);
		bean.setSocketIp("192.168.1.112");
		bean.setSocketPort(6789);
		return bean;
	}

	/** 默认配置 */
	public static String getDefaultSettingParamJson() {
		return JsonHelper.toJson(getDefaultSettingParam());
	}

	/** 获取配置 */
	public static SettingBean getSettingBean() {
		String setting_json = (String) SPUtils.get(ManagerAPP.getContext(),
				SP_CurrentSetting, getDefaultSettingParamJson());
		SettingBean bean = JsonHelper.parseObject(setting_json,
				SettingBean.class);
		return bean;
	}

	/** 更新配置 */
	public static void updateSettingBean(SettingBean bean) {
		SPUtils.put(ManagerAPP.getContext(), SP_CurrentSetting,
				JsonHelper.toJson(bean));
	}

	/** 获取登陆页Menu数据 */
	public static List<MenuBean> getLoginMenuBeans() {
		int strings[] = { R.string.common_setting, R.string.common_login_scan,
				R.string.common_login_swingcard, R.string.common_experience };
		int drawables[] = { R.drawable.login_sz_bg, R.drawable.login_smdl_bg,
				R.drawable.login_skdl_bg, R.drawable.login_lkty_bg };
		int images[] = { R.drawable.login_icon_sz, R.drawable.login_icon_smdl,
				R.drawable.login_icon_skdl, R.drawable.login_icon_ykdl };
		List<MenuBean> beans = new ArrayList<MenuBean>();
		SettingBean settingBean = getSettingBean();

		Boolean show[] = { true, true, true, true };
		if (!settingBean.isSupportTourist()) {
			show[3] = false;
		}
		switch (settingBean.getLoginType()) {
		case loginType_no:
			show[1] = false;
			show[2] = false;
			break;
		case loginType_scan:
			show[2] = false;
			break;
		case loginType_swingcard:
			show[1] = false;
			break;
		}

		for (int i = 0; i < 4; i++) {
			if (show[i]) {
				MenuBean loginMenuBean = new MenuBean();
				loginMenuBean.setDrawable(drawables[i]);
				loginMenuBean.setImage(images[i]);
				loginMenuBean.setName(strings[i]);
				beans.add(loginMenuBean);
			}
		}
		return beans;
	}

	/** 根据选中项，返回登录方式菜单设置 */
	public static List<MenuBean> getLoginTypeMenuBeans(boolean type_scan,
			boolean type_swingcard) {
		int name[] = { R.string.common_login_scan,
				R.string.common_login_swingcard };
		int drawables[] = { R.drawable.login_smdl_bg, R.drawable.login_skdl_bg };
		int images[] = { R.drawable.login_icon_smdl, R.drawable.login_icon_skdl };
		boolean select[] = { type_scan, type_swingcard };
		List<MenuBean> menuBeans = new ArrayList<MenuBean>();
		for (int i = 0; i < 2; i++) {
			MenuBean bean = new MenuBean();
			bean.setName(name[i]);
			bean.setSelected(select[i]);
			bean.setDrawable(drawables[i]);
			bean.setImage(images[i]);
			menuBeans.add(bean);
		}
		return menuBeans;
	}

	/** 根据选中项，返回菜单设置 */
	public static List<MenuBean> getSettingMenuBeans(int select_pos) {
		int defult_background = R.color.transparent;
		int defult_textcolor = R.color.white;
		int[] setting_color = { R.color.setting_pass,
				R.color.setting_login_type, R.color.setting_device,
				R.color.setting_language, R.color.setting_wifi,
				R.color.setting_tiyan };

		int[] image = { R.drawable.set_icon_csmm, R.drawable.set_icon_dlfs,
				R.drawable.set_icon_sbxh, R.drawable.set_icon_yy,
				R.drawable.set_icon_wifi, R.drawable.set_icon_tyms };
		int[] image2 = { R.drawable.set_icon_csmm2, R.drawable.set_icon_dlfs2,
				R.drawable.set_icon_sbxh2, R.drawable.set_icon_yy2,
				R.drawable.set_icon_wifi2, R.drawable.set_icon_tyms2 };

		int[] name = { R.string.common_setting_csmm,
				R.string.common_setting_dlfs, R.string.common_setting_sbxh,
				R.string.common_setting_xzyy, R.string.common_setting_wifisz,
				R.string.common_setting_tyms };

		List<MenuBean> menuBeans = new ArrayList<MenuBean>();

		for (int i = 0; i < 6; i++) {
			MenuBean bean = new MenuBean();
			bean.setName(name[i]);
			if (i == select_pos) {
				bean.setImage(image2[i]);
				bean.setTextcolor(defult_textcolor);
				bean.setDrawable(setting_color[i]);
			} else {
				bean.setImage(image[i]);
				bean.setTextcolor(setting_color[i]);
				bean.setDrawable(defult_background);
			}
			menuBeans.add(bean);
		}
		return menuBeans;
	}
}
